﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Duty.Model;

public partial class BasicBase_Duty_DutyReplace : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();

    }
    private void Bind()
    {
        DptYear.SelectedValue = DateTime.Now.Year.ToString();
        DptMonth.SelectedValue = DateTime.Now.Month.ToString();
        DptMonth_SelectedIndexChanged(DptMonth, new EventArgs());
        DptDay.SelectedValue = DateTime.Now.Day.ToString();
        BtnSearch_Click(BtnSearch,new EventArgs());

    }
    protected void GvwList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }

    protected void IBtnEdit_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName.Trim()!="" && e.CommandArgument != null)
        {           

            DutyOrderInfo Model = Yipusoft.Duty.BLL.DutyOrder.GetModel(int.Parse(e.CommandName.Trim()));
            PanelEdit.Visible = true;
            lblDutyType.Text = Yipusoft.Duty.BLL.DutyType.GetModel(Model.DutyType).DTName;
            lblName.Text = Model.UserName;
            lblDate.Text = Model.DutyDate.Value.ToShortDateString();
            BtnSave.CommandName = Model.ID.ToString();
            //判断当前用所在组
            string Dept = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
            if (Dept == null || Dept.Length < 5)
                return;

            string strSql = "";
            if (Dept.Substring(4, 1).Equals("0"))
                strSql += "  Left(DepartmentID,5)='" + Dept.Substring(0, 5) + "'";
            else
                strSql += "  DepartmentID='" + Dept + "'";

            this.DptNow.DataSource = Yipusoft.Duty.BLL.Member.GetList(" UserID in(Select UserID From MemberStation Where " + strSql + ") And IsDelete=0");
            this.DptNow.DataTextField = "UserName";
            this.DptNow.DataValueField = "UserID";
            this.DptNow.DataBind();

        }   
        
    }
    protected void DptMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        int Day= DateTime.DaysInMonth(int.Parse(DptYear.SelectedValue), int.Parse(DptMonth.SelectedValue));
        DptDay.Items.Clear();
        for (int i = 1; i <= Day; i++)
        {
            DptDay.Items.Add(new ListItem(i.ToString() + "日", i.ToString()));
        }
    }
    public bool GetBool()
    {
        bool bl = false;
        if (DptYear.SelectedValue == DateTime.Now.Year.ToString() && DptMonth.SelectedValue == DateTime.Now.Month.ToString() && DptDay.SelectedValue == DateTime.Now.Day.ToString())
        {
            bl = true;
        }
        return bl;
    }

    public string GetDutName( object DutyID)
    {
        DutyTypeInfo Info= Yipusoft.Duty.BLL.DutyType.GetModel(DutyID.ToString());
        return Info != null ? Info.DTName : "";
    }

    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        DateTime dt=new DateTime(int.Parse(DptYear.SelectedValue),int.Parse(DptMonth.SelectedValue),int.Parse(DptDay.SelectedValue));
        string strAnd="";
        if(TxtName.Text.Trim()!="")
            strAnd=" And UserName Like '%"+TxtName.Text.Trim()+"%'";

        //判断当前用所在组
        string Dept = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Dept == null || Dept.Length < 5)
            return;

        string strSql = " And DutyType in (Select DutyType From DutyType Where ";
        if (Dept.Substring(4, 1).Equals("0"))
            strSql += "  Left(DepartmentID,5)='" + Dept.Substring(0, 5) + "')";
        else
            strSql += "  DepartmentID='" + Dept + "')";

        GvwList.DataSource= Yipusoft.Duty.BLL.DutyOrder.GetList(" DutyDate='" + dt.ToShortDateString() + "'" + strAnd+strSql+" Order By DutyType");
        GvwList.DataBind();
    }
    protected void IBtnDelete_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName.Trim() != "" && e.CommandArgument != null)
        {
            Yipusoft.Duty.BLL.DutyOrder.Delete(" ID=" + e.CommandName + "");
            BtnSearch_Click(this.BtnSearch, new EventArgs());
            PanelEdit.Visible = false;
        }
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (BtnSave.CommandName.Trim() != "")
        {
            try
            {
                DutyOrderInfo info = Yipusoft.Duty.BLL.DutyOrder.GetModel(int.Parse(BtnSave.CommandName.Trim()));
                info.UserID = DptNow.SelectedValue;
                info.UserName = DptNow.SelectedItem.Text;

                int Flag = Yipusoft.Duty.BLL.DutyOrder.Update(info);
                BtnSearch_Click(this.BtnSearch, new EventArgs());
                if (Flag > 0)                
                    lblPromt.Text = "更新成功";
                
            }
            catch (Exception ex) { lblPromt.Text = ex.Message; };

           
        }
    }
}
